<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像 -->
      <div class="avatar-box">
        <h3>保险管理平台</h3>
      </div>
      <!-- 表单登录区域 -->
      <el-form
        label-width="0px"
        class="login-form"
        :model="loginForm"
        ref="loginFormRef"
        :rules="loginFormRules"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="loginForm.password" type="password"></el-input>
        </el-form-item>
        <el-select v-model="loginForm.value" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type @click="login1">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapActions } = createNamespacedHelpers("admin");
export default {
  data() {
    return {
      options: [
        {
          value: "0",
          label: "销售员",
        },
        {
          value: "1",
          label: "管理员",
        },
      ],
      loginForm: {
        username: "superadmin",
        password: "123",
        value: "1",
      },
      //表单的验证规则对象
      loginFormRules: {
        //验证用户名是否合法
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          {
            min: 6,
            max: 11,
            message: "长度在 6 到 11 个字符",
            trigger: "blur",
          },
        ],
        //验证密码是否合法
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 2,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {},
  methods: {
    //点击重置表单，重置登录表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    ...mapActions(["login"]),

    async login1() {
      var _this = this;
      const data = await _this.login(_this.loginForm);

      if (data.status) {
        if (data.value) {
          this.$router.push("/home");
        }else{
            this.$router.push("/saleSystem");
        }
      } else {
        alert("登录失败");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  background-image: url("../assets/img/login-bg.e2134055.jpg");
  background-size: 100%;
  height: 100%;
  .login-box {
    width: 350px;
    height: 288px;
    background: hsla(0, 0%, 100%, 0.3);
    border-radius: 3px;
    position: absolute;
    left: calc(50% - 175px);
    top: calc(50% - 144px);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .login-form {
      width: 90%;
      display: flex;
      flex-direction: column;
      padding: 0 20px;
      box-sizing: border-box;
    }
    .btns {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
